package com.krolik22.camel.learn;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TriggerPropagationAggregationStrategy implements AggregationStrategy {

	private static final Logger LOG = LoggerFactory.getLogger(TriggerPropagationAggregationStrategy.class);

	@Override
    public Exchange aggregate(final Exchange oldExchange, final Exchange newExchange) {
		
		LOG.info("aggregate({}, {}) called", oldExchange, newExchange);
		
        if (oldExchange == null) {
            return newExchange;
        }
        
        Message newIn = newExchange.getIn();
        String newBody = newIn.getBody(String.class);

        /**
        **/
        // check for stop command
        if ("GO".equalsIgnoreCase(newBody)) {
            return oldExchange;
        }
        
        String oldBody = oldExchange.getIn().getBody(String.class);

//        newIn.setBody(oldBody + newBody);
        oldExchange.getIn().setBody(oldBody + newBody);
        
//        Integer num3 = (num1 != null ? num1 : 0) + (num2 != null ? num2 : 0);
//        oldExchange.getIn().setBody(num3);

        return newExchange;
    }

	/**
	 public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
		   Message newIn = newExchange.getIn();
		   String oldBody = oldExchange.getIn().getBody(String.class);
		   String newBody = newIn.getBody(String.class);
		   newIn.setBody(oldBody + newBody);
		   return newExchange;
		 }
	 **/
	
}
